package com.qfedu.web;

import com.qfedu.pojo.*;
import com.qfedu.service.AccountService;
import com.qfedu.service.CardsService;
import com.qfedu.service.SaveService;
import com.qfedu.service.User_cardsService;
import com.qfedu.service.impl.AccountServiceImpl;
import com.qfedu.service.impl.CardsServiceImpl;
import com.qfedu.service.impl.SaveServiceImpl;
import com.qfedu.service.impl.User_cardsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/saveServlet")
public class SaveServlet extends BaseServlet{

    CardsService cardsService = new CardsServiceImpl();
    SaveService saveService = new SaveServiceImpl();
    User_cardsService user_cardsService = new User_cardsServiceImpl();
    AccountService accountService = new AccountServiceImpl();

    public void currentToFixed(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String cardnumber = request.getParameter("cardnumber");
        String money = request.getParameter("money");
        String time = request.getParameter("time");
        String paypassword = request.getParameter("paypassword");
        User user = (User)request.getSession().getAttribute("user");
        if(user!=null){
            BigDecimal money1 = new BigDecimal(money);
            Debit_card debit_card = saveService.queryByCardnumber(cardnumber);
            //判断卡号是否存在
            if(debit_card != null){
                //判断支付密码是否正确                                                                          //用户登录后，从sesion获得phone
                if(paypassword.equals(accountService.queryAccountByIdcode(accountService.queryIdcodeByPhone(user.getPhone()).getIdcode()).getPaypassword())){
                    //判断活期余额是否充足
                    if (debit_card.getMoney().compareTo(money1) >= 0) {
                        BigDecimal outmoney = debit_card.getMoney().subtract(money1);
                        BigDecimal inmoney = debit_card.getFixed_money().add(money1);
                        saveService.updateTime(cardnumber,time);
                        saveService.currentToFixed(debit_card,new Date(),outmoney,inmoney);
                        cardsService.updateCards(String.valueOf(outmoney),cardnumber);
                        request.getRequestDispatcher("/pages/savemoney/currentToFixed_success.jsp").forward(request, response);
                    } else {
                        request.setAttribute("msg","余额不足");
                        request.setAttribute("cardnumber",cardnumber);
                        //跳回开户页面
                        request.getRequestDispatcher("/pages/savemoney/currentToFixed.jsp").forward(request, response);
                    }
                }else {
                    request.setAttribute("msg","支付密码错误");
                    request.setAttribute("cardnumber",cardnumber);
                    //跳回存期页面
                    request.getRequestDispatcher("/pages/savemoney/currentToFixed.jsp").forward(request, response);
                }

            }else {
                request.setAttribute("msg","银行卡号输入错误");
                request.getRequestDispatcher("/pages/savemoney/currentToFixed.jsp").forward(request, response);
            }


        }else {
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
        }


    }

    public void fixedToCurrent(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String cardnumber = request.getParameter("cardnumber");
        String money = request.getParameter("money");
        String paypassword = request.getParameter("paypassword");
        User user = (User)request.getSession().getAttribute("user");
        if(user!=null){
            BigDecimal money1 = new BigDecimal(money);
            Debit_card debit_card = saveService.queryByCardnumber(cardnumber);
            if(debit_card != null){
                if(paypassword.equals(accountService.queryAccountByIdcode(accountService.queryIdcodeByPhone(user.getPhone()).getIdcode()).getPaypassword())){
                    if (debit_card.getFixed_money().compareTo(money1) >= 0) {
                        BigDecimal outmoney = debit_card.getFixed_money().subtract(money1);
                        Date date = new Date();
                        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date savedate = sdf.parse(debit_card.getSavedate());
                        long day = (date.getTime()-savedate.getTime())/86400000;
                        long time = new Long(debit_card.getTime());
                        BigDecimal profit = new BigDecimal(0);
                        BigDecimal profit1= new BigDecimal(0);
                        User_cards user_cards = user_cardsService.queryUser_cards(cardnumber);
                        MoneyRate moneyrate = saveService.findRate(user_cards.getBank_name(), debit_card.getTime());
                        if(time<=day){
                            //存够time得到的利息
                            profit1 = debit_card.getFixed_money().multiply(moneyrate.getRate());
                            //定期加上利息 乘 利率 乘 多存的时间 除以 time
                            profit = debit_card.getFixed_money().add(profit1).multiply(moneyrate.getRate()).multiply(new BigDecimal(day-time)).divide(BigDecimal.valueOf(time),2, BigDecimal.ROUND_HALF_UP);
                            //获取等于原有的加 利息 加取的定期
                            BigDecimal currentmoney = debit_card.getMoney().add(profit).add(profit1).add(money1);
                            BigDecimal fixedmoney = debit_card.getFixed_money().subtract(money1);
                            saveService.fixedToCurrent(debit_card,new Date(),currentmoney,fixedmoney);
                            cardsService.updateCards(String.valueOf(currentmoney),cardnumber);
                        }else {
                            profit = debit_card.getFixed_money().multiply(new BigDecimal(0.0035));
                            BigDecimal currentmoney = debit_card.getMoney().add(profit).add(money1);
                            BigDecimal fixedmoney = debit_card.getFixed_money().subtract(money1);
                            saveService.fixedToCurrent(debit_card,new Date(),currentmoney,fixedmoney);
                            cardsService.updateCards(String.valueOf(currentmoney),cardnumber);
                        }
                        request.getRequestDispatcher("/pages/savemoney/fixedToCurrent_success.jsp").forward(request, response);
                    } else {
                        request.setAttribute("msg","定期金额不足");
                        request.setAttribute("cardnumber",cardnumber);
                        //跳回开户页面
                        request.getRequestDispatcher("/pages/savemoney/fixedToCurrent.jsp").forward(request, response);
                    }
                }else {
                    request.setAttribute("msg","支付密码错误");
                    request.setAttribute("cardnumber",cardnumber);
                    //跳回存期页面
                    request.getRequestDispatcher("/pages/savemoney/fixedToCurrent.jsp").forward(request, response);

                }
            }else {
                request.setAttribute("msg","银行卡号输入错误");
                request.getRequestDispatcher("/pages/savemoney/fixedToCurrent.jsp").forward(request, response);
            }
        }else {

            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
        }


    }

    public void findCurrentMoney(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String cardnumber = request.getParameter("cardnumber");
        Debit_card debit_card = saveService.queryByCardnumber(cardnumber);
        if(debit_card==null){
            request.setAttribute("msg", "卡号信息有误！！");
            request.getRequestDispatcher("/pages/savemoney/currentToFixed.jsp").forward(request, response);
        }
        request.setAttribute("yu_e",debit_card.getMoney());
        request.setAttribute("cardnumber", cardnumber);
        request.getRequestDispatcher("/pages/savemoney/currentToFixed.jsp").forward(request, response);
    }



    public void findFixedMoney(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String cardnumber = request.getParameter("cardnumber");
        Debit_card debit_card = saveService.queryByCardnumber(cardnumber);
        if(debit_card==null){
            request.setAttribute("msg", "卡号信息有误！！");
            request.getRequestDispatcher("/pages/savemoney/fixedToCurrent.jsp").forward(request, response);
        }
        request.setAttribute("yu_e",debit_card.getFixed_money());
        request.setAttribute("cardnumber", cardnumber);
        request.getRequestDispatcher("/pages/savemoney/fixedToCurrent.jsp").forward(request, response);
    }




    }



